package com.baturu.vin.honda.cleanData;

import com.baturu.vin.honda.dal.dao.CleanDataDAO;
import com.baturu.vin.test.AbstractBaseCase;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;

/**
 * 本田数据清洗
 * Created by zhouchenghai
 * Modified by other
 */
public class HondaClean extends AbstractBaseCase {

    @Autowired
    private CleanDataDAO cleanDataDAO;

    @Test
    public void cleanData() throws Exception {
        readData(1);
        readData(2);
    }

    private void readData(int disk){
        String dir="M:/work/cleanData/honda/disk"+disk+"/20190312";
        File dirFile=new File(dir);
        if (dirFile.isDirectory()) {
            File[] files = dirFile.listFiles((dir1, name) -> name.endsWith(".sql")&&!name.contains("_out"));
            for (File file : files) {
                String inPath=file.getAbsolutePath();
                String name=file.getName();
                String outPath = file.getParent()+"/out";
                File outFile = new File(outPath);
                if (!outFile.exists()) {
                    outFile.mkdir();
                }
                String outSql=outPath+"/"+name.substring(0,name.indexOf("."))+"_out.sql";
                autoFixSql(inPath,outSql,disk);
            }
        }
    }


    private void autoFixSql(String inPath, String outPath, int disk){
        StringBuilder stringBuilder = new StringBuilder();
        IntWrapper lines = new IntWrapper();
        FileUtil.readFile(inPath, "UTF-8", (s, reader) -> {
            if (!StringUtils.isEmpty(s)) {
                lines.countAdd();
            }
        });
        IntWrapper intWrapper = new IntWrapper();
        FileUtil.readAndWriteFile(inPath, outPath, "UTF-8", new FileUtil.ReadAndWriteListener() {
            @Override
            public boolean onReadLine(String s, Writer writer) throws IOException {
                lines.countDown();
                if (StringUtils.isBlank(s)) {
                    return false;
                }
                if (s.contains("INSERT")) {
                    if (stringBuilder.length() <= 0) {
                        System.out.println(s.contains("\ufeff"));
                        int index1 = s.indexOf("O");
                        int index2 = s.indexOf(".");
                        int index3 = s.lastIndexOf(")");
                        String part1 = s.substring(0, index1 + 1).replace("\ufeff","").replace("INSERT","INSERT IGNORE");
                        String part2 = s.substring(index2 + 1, index3).replace("\"","`");
                        stringBuilder.append(part1).append(" ").append(part2).append(",disk) VALUES ");
                        writer.write(stringBuilder.toString());
                    } else {
                        return false;
                    }
                } else {
                    String replace = s.replace("\ufeff", "");
                    if (StringUtils.isBlank(replace)) {
                        return false;
                    }
                    s = "(" + s.substring(0, s.lastIndexOf(";"));
                    s = s.substring(0, s.lastIndexOf(")")) + "," + (disk)+ ")";
                    if (lines.getCount() == 0) {
                        s = s + ";";
                        writer.write(s);
                    } else if (intWrapper.getCount() < 5000) {
                        s = s + ",";
                        writer.write(s);
                        intWrapper.countAdd();
                    } else {
                        s = s + ";\r\n";
                        writer.write(s);
                        writer.write(stringBuilder.toString());
                        intWrapper.setCount(0);
                    }
                }
                return true;
            }
        });
        insertData(outPath);
    }


    private void insertData(String path){
        StringBuilder sb=new StringBuilder();
        FileUtil.readFile(path, "utf-8", new FileUtil.ReadLineListener() {
            @Override
            public void onReadLine(String s, Reader reader) throws IOException {
                sb.append(s);
                if (s.contains(";")){
                    if (sb.length()>0){
                        cleanDataDAO.insertSql(sb.toString());
                        sb.delete(0,sb.length());
                    }
                }
            }
        });
        if (sb.length()>0){
            cleanDataDAO.insertSql(sb.toString());
        }

    }


}
